package sy.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.SysUserUnitMappingDaoI;
import sy.model.SysUserUnitMapping;

@Repository("sysUserUnitMappingDao")
public class SysUserUnitMappingDaoImpl implements SysUserUnitMappingDaoI {
	
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}
	
	@Override
	public Serializable save(SysUserUnitMapping sysUserUnitMapping) {
		return getCurrentSession().save(sysUserUnitMapping);
	}

	@Override
	public void remove(SysUserUnitMapping sysUserUnitMapping) {
		// TODO Auto-generated method stub
		getCurrentSession().delete(sysUserUnitMapping);
	}

	@Override
	public void update(SysUserUnitMapping sysUserUnitMapping) {
		// TODO Auto-generated method stub
		getCurrentSession().update(sysUserUnitMapping);
	}

	@Override
	public List<SysUserUnitMapping> list() {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery("from SysUserUnitMapping").list();
	}

	@Override
	public SysUserUnitMapping getSysUserUnitMappingById(String id) {
		// TODO Auto-generated method stub
		String hql = "from SysUserUnitMapping where id = :id";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("id", id);
		List<SysUserUnitMapping> list = query.list();
		if (list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<SysUserUnitMapping> listSysUserUnitMappingByKey(String key) {
		// TODO Auto-generated method stub
		String hql = "from SysUserUnitMapping where sysUnit.id like :key or sysUser.id like :key";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("key", "%"+key+"%");
		List<SysUserUnitMapping> list = query.list();
		return list;
	}

	@Override
	public List<SysUserUnitMapping> listSysUserUnitMappingByOwnerId(String ownerId) {
		// TODO Auto-generated method stub
		String hql = "from SysUserUnitMapping where sysUser.id = :ownerId";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("ownerId", ownerId);
		List<SysUserUnitMapping> list = query.list();
		return list;
	}
	/**
	 * 批量添加实体
	 */
	@Override
	public Serializable saveSysUserUnitMappings(List<SysUserUnitMapping> sysUserUnitMapping) {
		// TODO Auto-generated method stub
		Session session = this.getCurrentSession();
		SysUserUnitMapping userUnit = null;
		for(int i = 0; i<sysUserUnitMapping.size(); i++){
			userUnit = sysUserUnitMapping.get(i);
			session.save(userUnit);
			if (i % 10 == 0) {  
				session.flush();  
				session.clear();  
			}  
		}
		return null;
	}
	
	/**
	 * 移除用户下的房址信息
	 */
	@Override
	public void removeByUser(String userId) {
		// TODO Auto-generated method stub
		String hql = "delete from SysUserUnitMapping where sysUser.id = :userId";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("userId", userId);
		query.executeUpdate();
	}

}
